// 列表初始化
import { ref } from 'vue'
import { getShortTrainSignListApi } from '@/api/sign/shortTrainSign'
import { deepClone } from '@/util'
import globalData from '@/staticData/globalData'

const resetForm = () => {
  return {
    pageIndex: 1,
    pageSize: 20
  }
}
const searchForm = ref(resetForm())
const formItems = ref([
  {
    field: 'emsClassId',
    type: 'auto-complete',
    props: { label: '班级ID' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 16
    }
  },
  {
    field: 'emsStudentId',
    type: 'auto-complete',
    props: { label: '学员ID' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 16
    }
  },
  {
    field: 'mobile',
    type: 'auto-complete',
    props: { label: '学员手机号' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      validateText: '请输入正确格式手机号'
    },
    validateRegex: /^1[3|4|5|6|7|8|9]\d{9}$/
  },
  {
    field: 'identityCard',
    type: 'auto-complete',
    props: { label: '学员身份证' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 32,
      validateText: '仅支持输入中文，英文字母'
    },
    validateRegex: /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
  },
  {
    field: 'contractStatus',
    type: 'select',
    props: { label: '合同状态' },
    attr: {
      placeholder: '请选择内容',
      multiple: true
    },
    options: globalData.SHORT_TRAIN_CONTRACT_STATUS_LIST
  }
])
const columns = ref([
  {
    label: '班级ID',
    prop: 'emsClassId'
  },
  {
    label: '班级名称',
    prop: 'emsClassName'
  },
  {
    label: '学员ID',
    prop: 'emsStudentId'
  },
  {
    label: '学员姓名',
    prop: 'emsStudentName'
  },
  {
    label: '学员手机号',
    prop: 'mobile'
  },
  {
    label: '学员身份证号',
    prop: 'identityCard'
  },
  {
    label: '合同编号',
    prop: 'contractCode',
    slots: { customRender: 'contractCode' }
  },
  {
    label: '合同状态',
    prop: 'contractStatus'
  },
  {
    label: '更新时间',
    prop: 'modifiedDateTime',
    slots: { customHeader: 'modifiedDatetime' }
  },
  {
    label: '定位白名单',
    prop: 'locationFlag',
    slots: { customRender: 'locationFlag' }
  },
  {
    label: '操作',
    prop: 'gender',
    slots: { customRender: 'action' }
  }
])
const businessLineList = ref([]) // 业务线数据
const total = ref(0)
const schemaTableRef = ref(null)

export default function useList() {
  // 控制按钮显示意、隐藏
  const resetFnCom = computed(() => {
    return status => {
      return status === '未开放' || status === '待签订'
    }
  })
  const openSignFnCom = computed(() => {
    return status => {
      return status === '未开放'
    }
  })
  const closeSignFnCom = computed(() => {
    return status => {
      return status === '待签订'
    }
  })
  const allowCancelFnCom = computed(() => {
    return status => {
      return status === '已签订'
    }
  })
  const canCancelFnCom = computed(() => {
    return status => {
      return status === '待作废'
    }
  })
  const offlineCancelFnCom = computed(() => {
    return status => {
      return status === '已签订'
    }
  })
  // 查询数据
  const loadData = async val => {
    if (Object.keys(searchForm.value).length === 0) {
      searchForm.value = resetForm()
    }
    let params = {}
    params = deepClone(searchForm.value)
    console.log('查询参数', params)
    // 处理修改时间
    if (params.modifiedDatetime?.length > 0) {
      params.modifiedDatetimeStart = params.modifiedDatetime[0]
      params.modifiedDatetimeEnd = params.modifiedDatetime[1]
      delete params.modifiedDatetime
    }
    // 处理数据排序
    if (val.order) {
      val.order === 'asc' ? params.modifyTimeSort = 1 : params.modifyTimeSort = 0
    }
    return getShortTrainSignListApi(params).then(res => {
      total.value = res.data.length
      console.log('查询数据', res)
      return {
        rows: res.data || []
      }
    })
  }

  // 搜索
  const search = async() => {
    if (schemaTableRef.value.localLoading) return
    schemaTableRef.value.refresh(true)
  }

  // 分页
  const handleSizeChange = async e => {
    searchForm.value.pageSize = e
    searchForm.value.pageIndex = 1
    search()
  }

  return {
    resetFnCom,
    openSignFnCom,
    closeSignFnCom,
    allowCancelFnCom,
    canCancelFnCom,
    offlineCancelFnCom,
    searchForm,
    formItems,
    columns,
    businessLineList,
    total,
    schemaTableRef,
    loadData,
    search,
    handleSizeChange
  }
}
